package com.ynkbny.util.sm4_v2;

/**  
 * @ClassName: SM4Utils
 * @Description: 
 * @author qianhua.chen  
 * @date 2021年6月5日  
 */
import org.apache.commons.lang3.ArrayUtils;

public class SM4Utils {
    public String secretKey = "";  
    private String iv = "";  
    public boolean hexString = false;  
    
    public SM4Utils()  
    {  
    }  

    public static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }
    public static String encodeCBC(String plainText, String key, String iv) {
        String sRet = null;

        try {
            byte[] bInput = hexStringToBytes(plainText);
            byte[] bIV = hexStringToBytes(iv);
            byte[] bKey = new byte[16];
            byte[] temp =hexStringToBytes(key);
            if (temp.length < 16) {
                for(int i = 0; i < temp.length; ++i) {
                    bKey[i] = temp[i];
                }
            } else {
                bKey = temp;
            }

            byte[] xorBlock = new byte[bInput.length];

            for(int j = 0; j < bInput.length; ++j) {
                xorBlock[j] = (byte)(bInput[j] ^ bIV[j]);
            }

            byte[] output = new byte[0];

            for(int i = 0; i < xorBlock.length; i += 16) {
                byte[] t = ArrayUtils.subarray(xorBlock, i, i + 16);
                output = ArrayUtils.addAll(output, (new SM4()).sm4(t, 16, bKey, 1));
            }

            sRet = byteToHex(output);
        } catch (Exception var13) {
        }

        return sRet;
    }
    public static String byteToHex(byte[] b) {
        if (b == null) {
            throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
        } else {
            String hs = "";
            String stmp = "";

            for(int n = 0; n < b.length; ++n) {
                stmp = Integer.toHexString(b[n] & 255);
                if (stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
            }
            return hs.toUpperCase();
        }
    }
    public static String formatPin2HexStr(String pin) {
        String sRet = null;
        if (pin != null) {
            StringBuffer buffer = new StringBuffer();
            int len = pin.length();
            if (len < 10) {
                buffer.append("0");
            }

            buffer.append(len).append(pin);

            while(buffer.length() < 16) {
                buffer.append("F");
            }

            try {
                sRet = byteToHex(buffer.toString().getBytes("UTF-8"));
            } catch (Exception var6) {
            }
        }

        return sRet;
    }
    public static void main(String[] args) {//
    
        String encodeCBC = encodeCBC(formatPin2HexStr("123456"),byteToHex("2li8C4K5sR4EUA3v".getBytes()),byteToHex("915323277755123517".substring(0, 16).getBytes()));
        System.out.println(encodeCBC);
        
	}
}
